//
// Created by gyc on 2023/5/19.
//
#include "../../common.h"


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode *trimBST(TreeNode *root, int low, int high) {

        while (root && (root->val < low || root->val > high)) {
            if (root->val < low) {
                root = root->right;
            } else {
                root = root->left;
            }
        }
        if (root == nullptr) {
            return nullptr;
        }


        TreeNode *t = root;
        while (t) {
            while (t->left && t->left->val < low) {
                t->left = t->left->right;
            }
            t = t->left;
        }

        t = root;
        while (t) {
            while (t->right && t->right->val > high) {
                t->right = t->right->left;
            }
            t = t->right;
        }

        return root;
    }
};

int main() {
    printf("hello");
}